//
//  PacketDispatchDelegate.m
//  TcpTest_Server
//
//  Created by scarlettsun on 12-7-28.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "PacketDispatchCenter.h"
#import "MainSocketDelegate.h"
#import "PacketDispatchDelegate.h"
#import "SockPacket.h"
#import "MacroComd.h"

@implementation PacketDispatchCenter
@synthesize delegate;

-(id) init
{
    logDelegate = [[LogInPacketCenter alloc] init];
    logOutDelegate = [[LogOutPacketCenter alloc] init];
    listenPortDelegate = [[ListenPortPacketCenter alloc] init];
    groupDelegate = [[GroupPacketCenter alloc] init];
    sysDelegate = [[SystemPacketCenter alloc] init];
    chatDelegate = [[PairChatPacketCenter alloc] init];
    buddyListDelegate = [[BuddyListPacketCenter alloc] init];
    registerDelegate = [[RegisterPacketCenter alloc] init];
    heartBeatDelegate = [[HeartBeatPacketCenter alloc] init];
    groupChatDelegate = [[GroupChatPacketCenter alloc] init];
    addNewFriendDelegate = [[AddNewFriendPacketCenter alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver:buddyListDelegate selector:@selector(notifySendBuddyList:) name:Notify_Send_BuddyList object:addNewFriendDelegate];
    
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:heartBeatDelegate];
}

-(void) doPacketDispatch:(SockPacket *)packet
{
//    @synchronized (self.delegate)
//    {
        UTF8Char * bytes = (UTF8Char *)[packet.packetContent bytes];
        NSUInteger fun =*((NSUInteger *)bytes);
        NSLog(@"cmd is %lu",fun);
        id<PacketDispatchDelegate> packetDelegate ;
        switch (fun) 
        {
            case CMD_FUN_LOG:
                packetDelegate = logDelegate;
                break;
            case CMD_FUN_GROUPCHAT:
                packetDelegate = groupChatDelegate;
                break;
            case CMD_FUN_REGISTER:
                packetDelegate = registerDelegate;
                break;
            case CMD_FUN_GROUP:
                packetDelegate = groupDelegate;
                break;
            case CMD_FUN_CHAT:
                packetDelegate = chatDelegate;
                break;
            case CMD_FUN_SYSTEM:
                packetDelegate = sysDelegate;
                break;
            case CMD_FUN_BUDDYLIST:
                packetDelegate = buddyListDelegate;
                break;
            case CMD_FUN_STATUSNOTIFY:
                packetDelegate = logOutDelegate;
                break;
            case CMD_FUN_HEART_BEAT:
                packetDelegate = heartBeatDelegate;
                break;
            case CMD_FUN_LISTENPORT:
                packetDelegate = listenPortDelegate;
                break;
            case CMD_FUN_ADD_NEW_FRIEND:
                packetDelegate = addNewFriendDelegate;
                break;
            default:
                packetDelegate = sysDelegate;
                break;
        }
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [packetDelegate MessageDispatch:packet];
        });
   // }
  
}
@end
